Node.js Decipher Reference

Node.js இல் குறிவிலக்கம் மற்றும் கிரிப்டோ தொகுதியைப் பயன்படுத்துவதற்கான முழுமையான வழிகாட்டி

Decipher Object

Decipher வகுப்பு Node.js இன் crypto தொகுதியின் ஒரு பகுதியாகும். இது Cipher வகுப்பைப் பயன்படுத்தி குறியாக்கம் செய்யப்பட்ட தரவை குறிவிலக்கம் செய்ய ஒரு வழியை வழங்குகிறது. Decipher நிகழ்வுகள் crypto.createDecipheriv() முறையைப் பயன்படுத்தி உருவாக்கப்படுகின்றன.

⚠️ குறிப்பு:

crypto.createDecipher() முறை பாதுகாப்பு கவலைகள் காரணமாக Node.js v10.0.0 முதல் மறக்கப்பட்டது. எப்போதும் crypto.createDecipheriv() ஐப் பயன்படுத்தவும், இதற்கு வெளிப்படையான துவக்க திசையன் (IV) தேவைப்படுகிறது.

Import Crypto Module

// Import the crypto module
const crypto = require('crypto');

// Create a decipher with createDecipheriv
const algorithm = 'aes-256-cbc';
const key = Buffer.from('your-encryption-key-in-hex', 'hex'); // 32 bytes for AES-256
const iv = Buffer.from('your-iv-in-hex', 'hex'); // 16 bytes for AES
const decipher = crypto.createDecipheriv(algorithm, key, iv);

Decipher Methods

முறை விளக்கம்
decipher.update(data[, inputEncoding][, outputEncoding]) தரவுடன் டிசைபரைப் புதுப்பிக்கிறது. inputEncoding வழங்கப்பட்டால், தரவு குறிப்பிட்ட குறியீட்டைப் பயன்படுத்தி ஒரு சரமாகும். outputEncoding குறிப்பிடப்பட்டால், திரும்பிய மதிப்பு குறிப்பிட்ட குறியீட்டைப் பயன்படுத்தி ஒரு சரமாக இருக்கும். இல்லையெனில், ஒரு Buffer திரும்பும்.
decipher.final([outputEncoding]) மீதமுள்ள எந்தவொரு குறிவிலக்கப்பட்ட உள்ளடக்கங்களையும் திரும்பப் பெறுகிறது. outputEncoding குறிப்பிடப்பட்டால், ஒரு சரம் திரும்பும்; இல்லையெனில், ஒரு Buffer திரும்பும்.
decipher.setAAD(buffer[, options]) ஒரு AEAD அல்காரிதத்தை (GCM அல்லது CCM போன்றவை) பயன்படுத்தும் போது, கூடுதல் அங்கீகரிக்கப்பட்ட தரவை (AAD) அமைக்கிறது.
decipher.setAuthTag(buffer) ஒரு AEAD அல்காரிதத்தைப் பயன்படுத்தும் போது, தரவின் ஒருமைப்பாட்டைச் சரிபார்க்கப் பயன்படுத்தப்படும் அங்கீகார டேக்கை அமைக்கிறது.
decipher.setAutoPadding([autoPadding]) autoPadding உண்மையாக இருக்கும் போது (இயல்புநிலை), முடிவிலிருந்து padding தானாகவே அகற்றப்படும். தரவு padding செய்யப்படவில்லை அல்லது கைமுறையாக unpadding செய்யப்பட்டால் முடக்கவும்.

Basic Decryption Example

பின்வரும் எடுத்துக்காட்டு AES-256-CBC உடன் குறியாக்கம் செய்யப்பட்ட தரவை எவ்வாறு குறிவிலக்கம் செய்வது என்பதை விளக்குகிறது:

const crypto = require('crypto');

// Encryption key and initialization vector
// In a real application, these would be securely stored and retrieved
const key = Buffer.from('1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef', 'hex');
const iv = Buffer.from('1234567890abcdef1234567890abcdef', 'hex');

// Encrypted text (from a previous encryption)
const encryptedText = '7a9c2c7157819144ede3cb9532263cb97c94a7b45d95163bb79aa1af55d4101d';

// Create a decipher
const algorithm = 'aes-256-cbc';
const decipher = crypto.createDecipheriv(algorithm, key, iv);

// Decrypt the data
let decrypted = decipher.update(encryptedText, 'hex', 'utf8');
decrypted += decipher.final('utf8');

console.log('Encrypted Text:', encryptedText);
console.log('Decrypted Text:', decrypted);

Complete Encryption/Decryption Example

குறியாக்கம் மற்றும் குறிவிலக்கம் இரண்டையும் காட்டும் முழுமையான எடுத்துக்காட்டு இங்கே:

const crypto = require('crypto');

// The message to encrypt
const message = 'This is a secret message that needs to be encrypted';

// Generate encryption key and IV
const key = crypto.randomBytes(32);
const iv = crypto.randomBytes(16);

// Encryption function using Cipher
function encrypt(text) {
  // Create cipher
  const cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
  
  // Encrypt data
  let encrypted = cipher.update(text, 'utf8', 'hex');
  encrypted += cipher.final('hex');
  
  return encrypted;
}

// Decryption function using Decipher
function decrypt(encryptedText) {
  // Create decipher with the same key and IV
  const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv);
  
  // Decrypt data
  let decrypted = decipher.update(encryptedText, 'hex', 'utf8');
  decrypted += decipher.final('utf8');
  
  return decrypted;
}

// Encrypt the message
const encryptedMessage = encrypt(message);
console.log('Original Message:', message);
console.log('Encrypted Message:', encryptedMessage);

// Decrypt the message
const decryptedMessage = decrypt(encryptedMessage);
console.log('Decrypted Message:', decryptedMessage);

// Verify the result
console.log('Decryption successful:', message === decryptedMessage);

Decrypting Binary Data

குறியாக்கம் செய்யப்பட்ட கோப்புகள் போன்ற பைனரி தரவை நீங்கள் குறிவிலக்கம் செய்யலாம்:

const crypto = require('crypto');
const fs = require('fs');

// Read the encryption key and IV (saved during encryption)
const key = Buffer.from(fs.readFileSync('encryption_key.txt', 'utf8'), 'hex');
const iv = Buffer.from(fs.readFileSync('encryption_iv.txt', 'utf8'), 'hex');

// Create read and write streams
const readStream = fs.createReadStream('encrypted.jpg.enc');
const writeStream = fs.createWriteStream('decrypted.jpg');

// Create decipher stream
const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv);

// Decrypt the file
readStream
  .pipe(decipher)
  .pipe(writeStream);

writeStream.on('finish', () => {
  console.log('File decryption completed');
});

Using AEAD Decryption

தொடர்புடைய தரவுடன் அங்கீகரிக்கப்பட்ட குறியாக்கம் (AEAD) இரண்டும் இரகசியத்தன்மை மற்றும் தரவு ஒருமைப்பாட்டை வழங்குகிறது. AEAD அல்காரிதத்துடன் குறியாக்கம் செய்யப்பட்ட தரவை குறிவிலக்கம் செய்வது எப்படி என்பது இங்கே:

const crypto = require('crypto');

// Encryption values (would be stored and retrieved securely in a real application)
const key = Buffer.from('1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef', 'hex');
const iv = Buffer.from('123456789012123456789012', 'hex'); // 12 bytes for GCM
const encryptedData = 'af56c283ae95963c1e1877adb558d860';
const authTag = Buffer.from('1234567890abcdef1234567890abcdef', 'hex');
const associatedData = 'Additional data that was authenticated';

// Create decipher using AES-GCM
const decipher = crypto.createDecipheriv('aes-256-gcm', key, iv);

// Set the Additional Authenticated Data (AAD)
decipher.setAAD(Buffer.from(associatedData));

// Set the authentication tag
decipher.setAuthTag(authTag);

try {
  // Decrypt the data
  let decrypted = decipher.update(encryptedData, 'hex', 'utf8');
  decrypted += decipher.final('utf8');
  
  console.log('Decrypted Text:', decrypted);
  console.log('Authentication verified successfully');
} catch (error) {
  console.error('Authentication failed:', error.message);
  // If authentication fails, the decryption will throw an error
}

AEAD Complete Example

AEAD குறியாக்கம் மற்றும் குறிவிலக்கத்தின் முழுமையான எடுத்துக்காட்டு இங்கே:

const crypto = require('crypto');

// Data to encrypt
const plainText = 'Secret message';
const associatedData = 'Additional data to authenticate';

// Generate key and IV (nonce)
const key = crypto.randomBytes(32);
const iv = crypto.randomBytes(12); // 12 bytes (96 bits) is recommended for GCM

// === ENCRYPTION ===
// Create cipher using AES-GCM
const cipher = crypto.createCipheriv('aes-256-gcm', key, iv);

// Set the Additional Authenticated Data (AAD)
cipher.setAAD(Buffer.from(associatedData));

// Encrypt the data
let encrypted = cipher.update(plainText, 'utf8', 'hex');
encrypted += cipher.final('hex');

// Get the authentication tag
const authTag = cipher.getAuthTag();

console.log('Encrypted Text:', encrypted);
console.log('Auth Tag (hex):', authTag.toString('hex'));
console.log('Associated Data:', associatedData);

// === DECRYPTION ===
// Create decipher
const decipher = crypto.createDecipheriv('aes-256-gcm', key, iv);

// Set the same AAD
decipher.setAAD(Buffer.from(associatedData));

// Set the authentication tag
decipher.setAuthTag(authTag);

try {
  // Decrypt
  let decrypted = decipher.update(encrypted, 'hex', 'utf8');
  decrypted += decipher.final('utf8');
  
  console.log('Decrypted Text:', decrypted);
  console.log('Decryption successful:', plainText === decrypted);
} catch (error) {
  console.error('Decryption failed:', error.message);
}

// === DECRYPTION WITH WRONG AUTH TAG (will fail) ===
try {
  const wrongDecipher = crypto.createDecipheriv('aes-256-gcm', key, iv);
  wrongDecipher.setAAD(Buffer.from(associatedData));
  
  // Set a wrong authentication tag
  const wrongAuthTag = crypto.randomBytes(16);
  wrongDecipher.setAuthTag(wrongAuthTag);
  
  // Try to decrypt
  let wrongDecrypted = wrongDecipher.update(encrypted, 'hex', 'utf8');
  wrongDecrypted += wrongDecipher.final('utf8'); // This will throw
  
  console.log('Should not reach here');
} catch (error) {
  console.error('Decryption with wrong auth tag failed (expected):', error.message);
}

Manual Padding Control

குறிவிலக்கத்திற்கான padding நடத்தையை நீங்கள் கைமுறையாகக் கட்டுப்படுத்தலாம்:

const crypto = require('crypto');

// Generate key and IV
const key = crypto.randomBytes(32);
const iv = crypto.randomBytes(16);

// Data to encrypt
const plainText = 'This is a test message';

// First encrypt with disabled auto padding
const cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
cipher.setAutoPadding(false);

// Manually pad to block size (16 bytes for AES)
function padToBlockSize(text, blockSize = 16) {
  const padLength = blockSize - (text.length % blockSize);
  return text + '\0'.repeat(padLength);
}

// Encrypt manually padded data
const paddedText = padToBlockSize(plainText);
let encrypted = cipher.update(paddedText, 'utf8', 'hex');
encrypted += cipher.final('hex');

// Now decrypt with auto padding disabled
function decryptWithPadding(encryptedText, usePadding) {
  const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv);
  decipher.setAutoPadding(usePadding);
  
  try {
    let decrypted = decipher.update(encryptedText, 'hex', 'utf8');
    decrypted += decipher.final('utf8');
    return decrypted;
  } catch (error) {
    return `Error: ${error.message}`;
  }
}

// With auto padding (default)
console.log('With auto padding:', decryptWithPadding(encrypted, true));

// Without auto padding (will include padding bytes)
const manualDecrypted = decryptWithPadding(encrypted, false);
console.log('Without auto padding:', manualDecrypted);

// Manually remove padding (trim null bytes)
function removeNullPadding(paddedText) {
  return paddedText.replace(/\0+$/, '');
}

console.log('With manual padding removal:', removeNullPadding(manualDecrypted));

Password-Based Decryption

கடவுச்சொல்-பெறப்பட்ட விசையைப் பயன்படுத்தி குறியாக்கம் செய்யப்பட்ட தரவை குறிவிலக்கம் செய்யவும்:

const crypto = require('crypto');

// Password and salt (from the encryption process)
const password = 'mysecretpassword';
const salt = Buffer.from('0123456789abcdef0123456789abcdef', 'hex');

// Encrypted data and IV from the encryption process
const encryptedData = '7a9c2c7157819144ede3cb9532263cb97c94a7b45d95163bb79aa1af55d4101d';
const iv = Buffer.from('0123456789abcdef0123456789abcdef', 'hex');

// Generate a key from the password
function getKeyFromPassword(password, salt) {
  // Use PBKDF2 to derive a key from the password
  return crypto.pbkdf2Sync(password, salt, 100000, 32, 'sha256');
}

// Decrypt data
function decryptWithPassword(encryptedText, password, salt, iv) {
  // Generate key from password
  const key = getKeyFromPassword(password, salt);
  
  // Create decipher
  const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv);
  
  // Decrypt data
  let decrypted = decipher.update(encryptedText, 'hex', 'utf8');
  decrypted += decipher.final('utf8');
  
  return decrypted;
}

try {
  // Decrypt the data
  const decryptedText = decryptWithPassword(encryptedData, password, salt, iv);
  console.log('Decrypted:', decryptedText);
} catch (error) {
  console.error('Decryption failed:', error.message);
}

// Try with wrong password
try {
  const wrongPassword = 'wrongpassword';
  const decryptedWithWrongPass = decryptWithPassword(encryptedData, wrongPassword, salt, iv);
  console.log('Decrypted with wrong password:', decryptedWithWrongPass);
} catch (error) {
  console.log('Decryption with wrong password failed (expected):', error.message);
}

Complete Password-Based Example

கடவுச்சொல்-அடிப்படையிலான குறியாக்கம் மற்றும் குறிவிலக்கத்தின் முழுமையான எடுத்துக்காட்டு இங்கே:

const crypto = require('crypto');

// Password and message
const password = 'mysecretpassword';
const message = 'This is a secret message protected by a password';

// Password-based encryption
function encryptWithPassword(text, password) {
  // Generate a random salt
  const salt = crypto.randomBytes(16);
  
  // Derive a key from the password
  const key = crypto.pbkdf2Sync(password, salt, 100000, 32, 'sha256');
  
  // Generate random IV
  const iv = crypto.randomBytes(16);
  
  // Create cipher
  const cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
  
  // Encrypt data
  let encrypted = cipher.update(text, 'utf8', 'hex');
  encrypted += cipher.final('hex');
  
  // Return all values needed for decryption
  return {
    salt: salt.toString('hex'),
    iv: iv.toString('hex'),
    encrypted: encrypted
  };
}

// Password-based decryption
function decryptWithPassword(encryptedInfo, password) {
  // Parse the values
  const salt = Buffer.from(encryptedInfo.salt, 'hex');
  const iv = Buffer.from(encryptedInfo.iv, 'hex');
  const encrypted = encryptedInfo.encrypted;
  
  // Derive the same key
  const key = crypto.pbkdf2Sync(password, salt, 100000, 32, 'sha256');
  
  // Create decipher
  const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv);
  
  // Decrypt data
  let decrypted = decipher.update(encrypted, 'hex', 'utf8');
  decrypted += decipher.final('utf8');
  
  return decrypted;
}

// Encrypt the message
const encryptedInfo = encryptWithPassword(message, password);
console.log('Encrypted information:', encryptedInfo);

// Decrypt the message
const decryptedMessage = decryptWithPassword(encryptedInfo, password);
console.log('Decrypted message:', decryptedMessage);
console.log('Decryption successful:', message === decryptedMessage);

// Try with wrong password
try {
  const wrongPassword = 'wrongpassword';
  const decryptedWithWrong = decryptWithPassword(encryptedInfo, wrongPassword);
  console.log('Decrypted with wrong password:', decryptedWithWrong);
} catch (error) {
  console.log('Decryption with wrong password failed (expected):', error.message);
}

Handling Errors

குறிவிலக்கம் பல்வேறு காரணங்களுக்காக தோல்வியடையலாம். இந்த பிழைகளை சரியாக கையாள்வது முக்கியம்:

const crypto = require('crypto');

// Generate key and IV
const key = crypto.randomBytes(32);
const iv = crypto.randomBytes(16);

// Create sample encrypted data
const cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
const validEncrypted = cipher.update('valid data', 'utf8', 'hex') + cipher.final('hex');

// Function to attempt decryption and handle errors
function tryDecrypt(encryptedText, decryptKey, decryptIv) {
  try {
    const decipher = crypto.createDecipheriv('aes-256-cbc', decryptKey, decryptIv);
    const decrypted = decipher.update(encryptedText, 'hex', 'utf8') + decipher.final('utf8');
    return { success: true, data: decrypted };
  } catch (error) {
    return { success: false, error: error.message };
  }
}

// Case 1: Correct key and IV
const result1 = tryDecrypt(validEncrypted, key, iv);
console.log('Case 1 (correct key and IV):', result1);

// Case 2: Wrong key
const wrongKey = crypto.randomBytes(32);
const result2 = tryDecrypt(validEncrypted, wrongKey, iv);
console.log('Case 2 (wrong key):', result2);

// Case 3: Wrong IV
const wrongIv = crypto.randomBytes(16);
const result3 = tryDecrypt(validEncrypted, key, wrongIv);
console.log('Case 3 (wrong IV):', result3);

// Case 4: Invalid encrypted data
const result4 = tryDecrypt('invalidhexdata', key, iv);
console.log('Case 4 (invalid data):', result4);

// Case 5: Corrupted encrypted data
const corruptedData = validEncrypted.substring(0, validEncrypted.length - 2);
const result5 = tryDecrypt(corruptedData, key, iv);
console.log('Case 5 (corrupted data):', result5);

Security Best Practices

மறக்கப்பட்ட createDecipher() க்கு பதிலாக createDecipheriv() ஐப் பயன்படுத்தவும்: நீங்கள் வெளிப்படையாக IV வழங்குகிறீர்கள் என்பதை உறுதிப்படுத்துகிறது.
பாதுகாப்பான விசை மற்றும் IV சேமிப்பு: குறியாக்க விசைகளைப் பாதுகாப்பாக சேமிக்கவும், விசை மேலாண்மை சேவையைப் பயன்படுத்துவதைக் கவனியுங்கள்.
குறிவிலக்கத்தைச் சரிபார்க்கவும்: முடிந்தால், குறிவிலக்கம் வெற்றிகரமாக இருந்ததைச் சரிபார்க்க ஒரு வழியைச் சேர்க்கவும் (எ.கா., அங்கீகரிக்கப்பட்ட குறியாக்கத்தைப் பயன்படுத்துதல்).
தேர்ந்தெடுக்கப்பட்ட-சைபர்டெக்ஸ்ட் தாக்குதல்களுக்கு எதிராகப் பாதுகாக்கவும்: குறியாக்கம் செய்யப்பட்ட தரவைத் திருத்துவதைத் தடுக்க அங்கீகரிக்கப்பட்ட குறியாக்கத்தை (AEAD) பயன்படுத்தவும்.
பிழைகளைப் பாதுகாப்பாகக் கையாளவும்: பிழை செய்திகளில் குறிவிலக்க செயல்முறை பற்றிய தகவல்களைக் கசியாமல் பார்த்துக் கொள்ளுங்கள்.
நிலையான-நேர ஒப்பீடுகளைப் பயன்படுத்தவும்: அங்கீகார டேக்குகள் அல்லது பிற உணர்திறன் மதிப்புகளைச் சரிபார்க்கும் போது, நேரத் தாக்குதல் தாக்குதல்களைத் தடுக்க நிலையான-நேர ஒப்பீடுகளைப் பயன்படுத்தவும்.

பயிற்சி

Node.js இல் பாதுகாப்பான குறிவிலக்கத்திற்காக பயன்படுத்தப்பட வேண்டிய சரியான முறையை தேர்வு செய்யவும்.

crypto.createDecipher()
✗ தவறு! "crypto.createDecipher()" முறை பாதுகாப்பு காரணங்களுக்காக மறக்கப்பட்டது
crypto.createDecipheriv()
✓ சரி! "crypto.createDecipheriv()" என்பது பாதுகாப்பான குறிவிலக்கத்திற்கான சரியான முறையாகும், இது வெளிப்படையான IV ஐ தேவைப்படுத்துகிறது
crypto.decrypt()
✗ தவறு! "crypto.decrypt()" என்பது Node.js இல் ஒரு செல்லுபடியாகும் முறை அல்ல
crypto.makeDecipher()
✗ தவறு! "crypto.makeDecipher()" என்பது Node.js இல் ஒரு செல்லுபடியாகும் முறை அல்ல